package com.doucat.support.component;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * File: PermissionManager.java
 * Author: wfy
 * Version: V100R001C01
 * Create: 2020/12/17 15:15
 * Changes (from 2020/12/17)
 * -----------------------------------------------------------------
 * 2020/12/17 : Create PermissionManager.java (wfy);
 * -----------------------------------------------------------------
 */
public class PermissionManager {
    private Activity activity;

    public PermissionManager(Activity activity) {
        this.activity = activity;
    }

    /**********************************************************************************************************/
    protected String TAG=getClass().getSimpleName();

    public static final int REQUEST_CODE_PERMISSION = 0x00099;
    //保存当前权限获取状态的值
    protected boolean hasPermission =false;
    /**
     * 请求权限
     *
     * @param permissions 请求的权限
     */
    public void requestPermission(String[] permissions) {
        if (checkPermissions(permissions)) {
            permissionSuccess(REQUEST_CODE_PERMISSION);
        } else {
            List<String> needPermissions = getDeniedPermissions(permissions);
            ActivityCompat.requestPermissions(activity, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE_PERMISSION);
        }
    }

    /**
     * 检测所有的权限是否都已授权
     *
     * @param permissions
     * @return
     */
    private boolean checkPermissions(String[] permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }

        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) !=
                    PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取权限集中需要申请权限的列表
     *
     * @param permissions
     * @return
     */
    private List<String> getDeniedPermissions(String[] permissions) {
        List<String> needRequestPermissionList = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) !=
                    PackageManager.PERMISSION_GRANTED ||
                    ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
                needRequestPermissionList.add(permission);
            }
        }
        return needRequestPermissionList;
    }


    /**
     * 确认所有的权限是否都已授权
     *
     * @param grantResults
     * @return
     */
    public boolean verifyPermissions(int[] grantResults) {
        if(grantResults.length==0){
            return false;
        }
        for (int grantResult : grantResults) {
            if (grantResult != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取权限成功
     *
     * @param requestCode
     */
    private void permissionSuccess(int requestCode) {
        Log.d(TAG, "获取权限成功=" + requestCode);
        hasPermission =true;
        if(permissionListener!=null){
            permissionListener.permissionSuccess(requestCode);
        }
    }

    /**
     * 权限获取失败
     * @param requestCode
     */
    private void permissionFail(int requestCode) {
        Log.d(TAG, "获取权限失败=" + requestCode);
        hasPermission =false;
        if(permissionListener!=null){
            permissionListener.permissionFail(requestCode);
        }
    }

    /**
     * 权限操作后结果处理
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode ==PermissionManager.REQUEST_CODE_PERMISSION) {
            if (verifyPermissions(grantResults)) {
                permissionSuccess(requestCode);
            } else {
                permissionFail(requestCode);
            }
        }
    }

    public interface PermissionListener {
        void permissionSuccess(int requestCode);
        void permissionFail(int requestCode);
    }

    private PermissionListener permissionListener;

    public PermissionListener getPermissionListener() {
        return permissionListener;
    }

    public void setPermissionListener(PermissionListener permissionListener) {
        this.permissionListener = permissionListener;
    }
}
